//
//  WCProjectMenuController.m
//  WabbitCode
//
//  Created by William Towe on 1/29/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCProjectMenuController.h"
#import "WCProject.h"
#import "WCDocumentController.h"
#import "WCBuildTarget.h"
#import "WCBuildConfiguration.h"
#import "WCBuildTargetsGroup.h"
#import "WCMacros.h"
#import "NSMenu+WCExtensions.h"


static const NSInteger kSetActiveBuildTargetMenuItemTag = 100;
static const NSInteger kSetActiveBuildConfigurationMenuItemTag = 101;

@implementation WCProjectMenuController

- (BOOL)validateMenuItem:(NSMenuItem *)item {
	SEL action = [item action];
	
	if (action == @selector(editActiveTargetAction:)) {
		WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
		if (!project) {
			[item setTitle:NSLocalizedString(@"Edit Active Target",@"Edit Active Target")];
			return NO;
		}
		
		WCBuildTarget *target = [project activeBuildTarget];
		[item setTitle:[NSString stringWithFormat:NSLocalizedString(@"Edit Active Target \"%@\"",@"Edit Active Target \"%@\""), [target name]]];
	}
	else if (action == @selector(editActiveConfigurationAction:)) {
		WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
		if (!project) {
			[item setTitle:NSLocalizedString(@"Edit Active Configuration",@"Edit Active Configuration")];
			return NO;
		}
		
		WCBuildConfiguration *config = [project activeBuildConfiguration];
		[item setTitle:[NSString stringWithFormat:NSLocalizedString(@"Edit Active Configuration \"%@\"",@"Edit Active Configuration \"%@\""), [config name]]];
	}
	return YES;
}
#pragma mark NSMenu Delegate
- (void)menuNeedsUpdate:(NSMenu *)menu {
	[menu removeAllItems];
	
	WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
	if (!project) {
		[menu addItemWithTitle:NSLocalizedString(@"No Project",@"No Project") action:NULL keyEquivalent:@""];
		return;
	}
	
	NSUInteger index = 0;
	if ([[[[menu supermenu] itemWithTag:kSetActiveBuildTargetMenuItemTag] submenu] isEqualTo:menu]) {
		WCBuildTarget *activeTarget = [project activeBuildTarget];
		for (WCBuildTarget *target in [[project buildTargetsGroup] childNodes]) {
			NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:[target name] action:@selector(_setActiveBuildTargetAction:) keyEquivalent:@""] autorelease];
			[item setImage:[target icon]];
			[item setTag:index++];
			[item setState:(activeTarget == target)?NSOnState:NSOffState];
			[item setTarget:self];
			[menu addItem:item];
		}
	}
	else if ([[[[menu supermenu] itemWithTag:kSetActiveBuildConfigurationMenuItemTag] submenu] isEqualTo:menu]) {
		WCBuildConfiguration *activeConfig = [project activeBuildConfiguration];
		for (WCBuildConfiguration *config in [[project activeBuildTarget] buildConfigurations]) {
			NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:[config name] action:@selector(_setActiveBuildConfigurationAction:) keyEquivalent:@""] autorelease];
			[item setImage:[config icon]];
			[item setTag:index++];
			[item setState:(activeConfig == config)?NSOnState:NSOffState];
			[item setTarget:self];
			[menu addItem:item];
		}
	}
}
#pragma mark *** Public Methods ***
+ (WCProjectMenuController *)sharedInstance; {
	static id sharedInstance = nil;
	if (!sharedInstance)
		sharedInstance = [[[self class] alloc] init];
	return sharedInstance;
}
#pragma mark IBActions
- (IBAction)editActiveTargetAction:(id)sender; {
	WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
	[project showGetInfoWindowForItem:[project activeBuildTarget]];
}

- (IBAction)editActiveConfigurationAction:(id)sender; {
	WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
	[project showGetInfoWindowForItem:[project activeBuildConfiguration]];
}
#pragma mark *** Private Methods ***
- (IBAction)_setActiveBuildTargetAction:(id)sender {
	WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
	[project setActiveBuildTarget:[[[project buildTargetsGroup] childNodes] objectAtIndex:[sender tag]]];
}

- (IBAction)_setActiveBuildConfigurationAction:(id)sender {
	WCProject *project = [[WCDocumentController sharedDocumentController] currentProject];
	[project setActiveBuildConfiguration:[[[project activeBuildTarget] buildConfigurations] objectAtIndex:[sender tag]]];
}
@end
